package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dto.GeMyFeedbackDTO;
import com.htgd.gardenexpo.entity.*;
import com.htgd.gardenexpo.page.FeedbackQuery;
import com.htgd.gardenexpo.page.FoodQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.GeMyFeedbackService;
import com.htgd.gardenexpo.service.GeMyFeedbackUploadService;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.vo.FeedbackList;
import com.htgd.gardenexpo.vo.GeConPlantVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.Current;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.htgd.gardenexpo.utils.RSAUtil.decryption;

/**
 * <p>
 * 反馈表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-09
 */
@RestController
@RequestMapping("/api/feedback")
@Api(tags = "反馈")
@Slf4j
public class GeMyFeedbackController {

    @Autowired
    private GeMyFeedbackService geMyFeedbackService;
    @Autowired
    private GeMyFeedbackUploadService geMyFeedbackUploadService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add (@Validated @RequestBody GeMyFeedbackDTO geMyFeedbackDTO,
                                         HttpServletRequest request){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            GeMyFeedback geMyFeedback = new GeMyFeedback();
            BeanUtils.copyProperties(geMyFeedbackDTO,geMyFeedback);
            String accessToken = request.getHeader("Access_token");
            String username = TokenUtil.getUsernameFromToken(accessToken);
            geMyFeedback.setOpenId(username);
            geMyFeedback.setCreateTime(LocalDateTime.now());
            geMyFeedbackService.save(geMyFeedback);
            //上传文件
            String uploadIds=geMyFeedback.getUploads();
            if (uploadIds!=null&&!uploadIds.equals("")){
                for (String uploadId:uploadIds.split(",")){
                    GeMyFeedbackUpload geMyFeedbackUpload = new GeMyFeedbackUpload();
                    geMyFeedbackUpload.setUrl(uploadId);
                    geMyFeedbackUpload.setFeedbackId(geMyFeedback.getId());
                    geMyFeedbackUploadService.save(geMyFeedbackUpload);
                }
            }
            result.setCode(200);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/addMini")
    @ApiOperation("小程序添加")
    public CommonResult<JSONObject> addMini (@RequestBody String encryptedText){
        String decryption = "";
        try {
            JSONObject request = JSON.parseObject(encryptedText);
            decryption = decryption(encryptedText);

            JSONObject key = JSONObject.parseObject(decryption);
            GeMyFeedbackDTO geMyFeedback = new GeMyFeedbackDTO();
            geMyFeedback.setContent(request.getString("content"));
            geMyFeedback.setPhone(key.getString("phone"));
            geMyFeedback.setEmail(request.getString("email"));
            geMyFeedback.setName(request.getString("name"));
            geMyFeedback.setType(request.getString("type"));
            geMyFeedback.setSecondType(request.getString("secondType"));
            geMyFeedback.setUploads(request.getString("uploads"));
            CommonResult<JSONObject> result = add(geMyFeedback,null);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            CommonResult<JSONObject> result = new CommonResult<>();
            result.setCode(500);
            result.setMessage(e.getMessage());
            return result;
        }

    }

    @PostMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeMyFeedback>> list(@RequestBody FeedbackQuery feedbackQuery) {
        CommonResult<List<GeMyFeedback>> result = new CommonResult<>();
        try {
            PageInfo<GeMyFeedback> list=geMyFeedbackService.queryList(feedbackQuery);
            result.setCode(200);
            result.setData(list.getList());
            result.setMessage(String.valueOf(list.getTotal()));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    public CommonResult<GeMyFeedback> details(Integer id) {
        CommonResult<GeMyFeedback> result = new CommonResult<>();
        try {
            GeMyFeedback geMyFeedback=geMyFeedbackService.details(id);
            result.setCode(200);
            result.setData(geMyFeedback);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }


    @GetMapping(value = "/getFeedbackByOpenId")
    @ApiOperation("获取反馈列表")
    public CommonResult<List<GeMyFeedback>> getFeedbackByOpenId(FeedbackQuery feedbackQuery,
                                                                HttpServletRequest request) {
        CommonResult<List<GeMyFeedback>> result = new CommonResult<>();
        try {
            String accessToken = request.getHeader("Access_token");
            String openId = TokenUtil.getUsernameFromToken(accessToken);
            PageInfo<GeMyFeedback> feedbackLists = geMyFeedbackService.getFeedbackByOpenId(openId,feedbackQuery);
            result.setCode(200);
            result.setData(feedbackLists.getList());
            result.setMessage(String.valueOf(feedbackLists.getTotal()));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/reply")
    @ApiOperation("回复")
    public CommonResult<JSONObject> reply(@RequestBody GeMyFeedback geMyFeedback) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geMyFeedbackService.updateById(geMyFeedback);
            result.setCode(200);
            result.setMessage("回复成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/getCount")
    @ApiOperation("获取反馈数量")
    public CommonResult<Map<String,Long>> getCount() {
        CommonResult<Map<String,Long>> result = new CommonResult<>();
        try {
            Map<String, Long> stringLongMap = new HashMap<>();
            Long count = geMyFeedbackService.getCount();
            stringLongMap.put("count",count);
            result.setCode(200);
            result.setMessage("获取成功");
            result.setData(stringLongMap);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/getConsultationAndSuggestionNums")
    @ApiOperation("获取咨询建议数量")
    public CommonResult getConsultationAndSuggestionNums() {
        log.info("综管-获取咨询建议数量");
        return geMyFeedbackService.getConsultationAndSuggestionNums();
    }

}
